home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZGeometry.cpp < prev    next >
Text File  |  1997-07-24  |  9KB  |  333 lines

  1. /*
  2.  *  File:       ZGeometry.h
  3.  *  Summary:    Point, size, and rectangle classes.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <4>      6/17/97    JDJ        TLongRect::MapTo uses longs instead of shorts.
  12.  *         <3>      3/19/97    JDJ        Pin methods pin to botRight instead of botRight
  13.  *                                    minus one.
  14.  *         <2>      3/11/97    JDJ        Made Pin methods const.
  15.  *         <1>      1/14/96    JDJ        Created.
  16.  */
  17.  
  18. #include <ZGeometry.h>
  19.  
  20. #include <Fp.h>
  21.  
  22. #include <ZNumbers.h>
  23.  
  24.  
  25. //-----------------------------------
  26. //    Constants
  27. //
  28. const TPoint         kZeroPt(0, 0);
  29. const TSize          kZeroSize(0, 0);
  30. const TRect          kZeroRect(0, 0, 0, 0);
  31.  
  32. const TLongPoint    kLongZeroPt(0, 0);
  33. const TLongSize      kLongZeroSize(0, 0);
  34. const TLongRect      kLongZeroRect(0, 0, 0, 0);
  35.  
  36.  
  37. //-----------------------------------
  38. //    STL specializations
  39. //
  40. DEFINE_STL_FUNCTIONS(TPoint);
  41. DEFINE_STL_FUNCTIONS(TSize);
  42. DEFINE_STL_FUNCTIONS(TRect);
  43. DEFINE_STL_FUNCTIONS(TLongPoint);
  44. DEFINE_STL_FUNCTIONS(TLongSize);
  45. DEFINE_STL_FUNCTIONS(TLongRect);
  46.  
  47.  
  48. // ===================================================================================
  49. //    class TPoint
  50. // ===================================================================================
  51.  
  52. //---------------------------------------------------------------
  53. //
  54. // TPoint::operator*
  55. //
  56. //---------------------------------------------------------------
  57. TPoint operator*(short num, const TPoint& pt)
  58. {
  59.     return TPoint((short) (pt.h*num), (short) (pt.v*num));
  60. }
  61.  
  62. #pragma mark -
  63.  
  64. // ===================================================================================
  65. //    class TSize
  66. // ===================================================================================
  67.  
  68. //---------------------------------------------------------------
  69. //
  70. // TSize::operator*
  71. //
  72. //---------------------------------------------------------------
  73. TSize operator*(short num, const TSize& size)
  74. {
  75.     return TSize((short) (size.width*num), (short) (size.height*num));
  76. }
  77.  
  78. #pragma mark -
  79.  
  80. // ===================================================================================
  81. //    class TRect
  82. // ===================================================================================
  83.  
  84. //---------------------------------------------------------------
  85. //
  86. // TRect::operator|
  87. //
  88. //---------------------------------------------------------------
  89. TRect TRect::operator|(const TRect& rect) const
  90. {
  91.     TRect returnRect;
  92.  
  93.     if (this->IsEmpty())
  94.         returnRect = rect;
  95.         
  96.     else if (rect.IsEmpty())
  97.         returnRect = *this;
  98.         
  99.     else {
  100.         returnRect.top = Min(top, rect.top);
  101.         returnRect.left = Min(left, rect.left);
  102.         returnRect.bottom = Max(bottom, rect.bottom);
  103.         returnRect.right = Max(right, rect.right);
  104.     }
  105.     
  106.     return returnRect; 
  107. }
  108.  
  109.  
  110. //---------------------------------------------------------------
  111. //
  112. // TRect::operator&
  113. //
  114. //---------------------------------------------------------------
  115. TRect TRect::operator&(const TRect& rect) const
  116. {
  117.     TRect returnRect;
  118.     
  119.     returnRect.top = Max(top, rect.top);
  120.     returnRect.left = Max(left, rect.left);
  121.     returnRect.bottom = Min(bottom, rect.bottom);
  122.     returnRect.right = Min(right, rect.right);
  123.     
  124.     if (left > right || top > bottom)
  125.         returnRect = kZeroRect;
  126.     
  127.     return returnRect;
  128.  
  129.  
  130. //---------------------------------------------------------------
  131. //
  132. // TRect::MapTo
  133. //
  134. //---------------------------------------------------------------
  135. void TRect::MapTo(const TRect& container, double maxScaleFactor)
  136. {    
  137.     ASSERT(maxScaleFactor > 0.0);
  138.     
  139.     short srcHeight = this->GetHeight();
  140.     short srcWidth  = this->GetWidth();
  141.  
  142.     short destHeight = container.GetHeight();
  143.     short destWidth  = container.GetWidth();
  144.  
  145.     double    heightScale    = (double) destHeight/srcHeight;
  146.     double    widthScale    = (double) destWidth/srcWidth;
  147.     double    scale        = (widthScale < heightScale) ? widthScale : heightScale;
  148.     double    maxScale    = sqrt(maxScaleFactor);
  149.  
  150.     if (scale > maxScale)
  151.         scale = maxScale;
  152.  
  153.     short aspectHeight = (short) (srcHeight*scale + 0.5);
  154.     short aspectWidth  = (short) (srcWidth*scale + 0.5);
  155.  
  156.     ASSERT(aspectHeight <= destHeight);
  157.     ASSERT(aspectWidth <= destWidth);
  158.  
  159.     top     = (short) ((container.top + container.bottom - aspectHeight)/2);
  160.     left = (short) ((container.left + container.right - aspectWidth)/2);
  161.  
  162.     bottom = (short) (top + aspectHeight);
  163.     right  = (short) (left + aspectWidth);
  164. }
  165.  
  166.  
  167. //---------------------------------------------------------------
  168. //
  169. // TRect::Pin
  170. //
  171. //---------------------------------------------------------------
  172. TPoint TRect::Pin(const TPoint& pt) const
  173. {
  174.     TPoint result = pt;
  175.     
  176.     if (result.h < left)
  177.         result.h = left;
  178.     else if (result.h > right)
  179.         result.h = right;
  180.     
  181.     if (result.v < top)
  182.         result.v = top;
  183.     else if (result.v > bottom)
  184.         result.v = bottom;
  185.     
  186.     return result;
  187. }
  188.  
  189. #pragma mark -
  190.  
  191. // ===================================================================================
  192. //    class TLongPoint
  193. // ===================================================================================
  194.  
  195. //---------------------------------------------------------------
  196. //
  197. // TLongPoint::operator*
  198. //
  199. //---------------------------------------------------------------
  200. TLongPoint operator*(long num, const TLongPoint& pt)
  201. {
  202.     return TLongPoint(pt.h*num, pt.v*num);
  203. }
  204.  
  205.  
  206. // ===================================================================================
  207. //    class TLongSize
  208. // ===================================================================================
  209.  
  210. //---------------------------------------------------------------
  211. //
  212. // TLongSize::operator*
  213. //
  214. //---------------------------------------------------------------
  215. TLongSize operator*(long num, const TLongSize& size)
  216. {
  217.     return TLongSize(size.width*num, size.height*num);
  218. }
  219.  
  220. #pragma mark -
  221.  
  222. // ===================================================================================
  223. //    class TLongRect
  224. // ===================================================================================
  225.  
  226. //---------------------------------------------------------------
  227. //
  228. // TLongRect::operator|
  229. //
  230. //---------------------------------------------------------------
  231. TLongRect TLongRect::operator|(const TLongRect& rect) const
  232. {
  233.     TLongRect returnRect;
  234.  
  235.     if (this->IsEmpty())
  236.         returnRect = rect;
  237.         
  238.     else if (rect.IsEmpty())
  239.         returnRect = *this;
  240.         
  241.     else {
  242.         returnRect.top = Min(top, rect.top);
  243.         returnRect.left = Min(left, rect.left);
  244.         returnRect.bottom = Max(bottom, rect.bottom);
  245.         returnRect.right = Max(right, rect.right);
  246.     }
  247.     
  248.     return returnRect; 
  249. }
  250.  
  251.  
  252. //---------------------------------------------------------------
  253. //
  254. // TLongRect::operator&
  255. //
  256. //---------------------------------------------------------------
  257. TLongRect TLongRect::operator&(const TLongRect& rect) const
  258. {
  259.     TLongRect returnRect;
  260.     
  261.     returnRect.top = Max(top, rect.top);
  262.     returnRect.left = Max(left, rect.left);
  263.     returnRect.bottom = Min(bottom, rect.bottom);
  264.     returnRect.right = Min(right, rect.right);
  265.     
  266.     if (left > right || top > bottom)
  267.         returnRect = kLongZeroRect;
  268.     
  269.     return returnRect;
  270.  
  271.  
  272. //---------------------------------------------------------------
  273. //
  274. // TLongRect::MapTo
  275. //
  276. //---------------------------------------------------------------
  277. void TLongRect::MapTo(const TRect& container, double maxScaleFactor)
  278. {    
  279.     ASSERT(maxScaleFactor > 0.0);
  280.     
  281.     long srcHeight = this->GetHeight();
  282.     long srcWidth  = this->GetWidth();
  283.  
  284.     short destHeight = container.GetHeight();
  285.     short destWidth  = container.GetWidth();
  286.  
  287.     double    heightScale    = (double) destHeight/srcHeight;
  288.     double    widthScale    = (double) destWidth/srcWidth;
  289.     double    scale        = (widthScale < heightScale) ? widthScale : heightScale;
  290.     double    maxScale    = sqrt(maxScaleFactor);
  291.  
  292.     if (scale > maxScale)
  293.         scale = maxScale;
  294.  
  295.     long aspectHeight = (long) (srcHeight*scale + 0.5);
  296.     long aspectWidth  = (long) (srcWidth*scale + 0.5);
  297.  
  298.     ASSERT(aspectHeight <= destHeight);
  299.     ASSERT(aspectWidth <= destWidth);
  300.  
  301.     top     = (container.top + container.bottom - aspectHeight)/2;
  302.     left = (container.left + container.right - aspectWidth)/2;
  303.  
  304.     bottom = top + aspectHeight;
  305.     right  = left + aspectWidth;
  306. }
  307.  
  308.  
  309. //---------------------------------------------------------------
  310. //
  311. // TLongRect::Pin
  312. //
  313. //---------------------------------------------------------------
  314. TLongPoint TLongRect::Pin(const TLongPoint& pt) const
  315. {
  316.     TLongPoint result = pt;
  317.     
  318.     if (result.h < left)
  319.         result.h = left;
  320.     else if (result.h > right)
  321.         result.h = right;
  322.     
  323.     if (result.v < top)
  324.         result.v = top;
  325.     else if (result.v > bottom)
  326.         result.v = bottom;
  327.     
  328.     return result;
  329. }
  330.  
  331.